<!-- 运费配置 -->
<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="300px" :rules="rules" v-loading="listLoading">
      <el-form-item label="运费满减所需最低消费" prop="litemall_express_freight_min">
        <el-input v-model="form.litemall_express_freight_min" />
      </el-form-item>
      <el-form-item label="运费满减不足所需运费" prop="litemall_express_freight_value">
        <el-input v-model="form.litemall_express_freight_value" />
      </el-form-item>
      <el-form-item>
        <el-button @click="onCancel">取消</el-button>
        <el-button type="primary" @click="onSubmit">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getList } from "@/api/pzgl/express";
import { submit } from "@/api/pzgl/express";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      listLoading: true,
      form: {},
      rules: {
        litemall_express_freight_min: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        litemall_express_freight_value: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getList().then((response) => {
        console.log(response);
        this.form = response.data;
        this.listLoading = false;
      });
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          submit(this.form).then((response) => {
            // 传递表单数据给 submit 函数
            console.log(response);
            this.$notify({
              title: "成功",
              message: "运费配置修改成功",
              type: "success",
            });
            this.listLoading = false;
            // 可能需要重置表单或执行其他操作
          }).catch((error) => {
            console.error("Error during submission:", error);
            this.$notify({
              title: "错误",
              message: "运费配置失败",
              type: "success",
            });
            this.listLoading = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    onCancel() {
      this.$message({
        message: "cancel!",
        type: "warning",
      });
    },
  },
};
</script>
